package models

import (
	_"github.com/go-sql-driver/mysql"
	"github.com/astaxie/beego/orm"
)

//数据库表对应的实体类
type UserInfo struct {
	Id int64
	Username string
	Password string
}

var (db orm.Ormer)

func init(){
	orm.Debug= true
	orm.RegisterDataBase("default","mysql","root:root@tcp(localhost:3306)/test?charset=utf8",30)
	orm.RegisterModel(new(UserInfo))
	db = orm.NewOrm()
}

func AddUser(user_info *UserInfo) (int64, error) {

	id, err := db.Insert(user_info)
	return id, err
}

func ReadUserInfo(users *[]UserInfo)  {
	qb,_ := orm.NewQueryBuilder("mysql")

	qb.Select("*").From("user_info")

	sql :=qb.String()

	db.Raw(sql).QueryRows(users)
}
